package com.rc.saas.tenant.mq;

import com.alibaba.fastjson.JSON;
import com.rc.saas.tenant.service.log.TenantLogService;
import com.rc.saas.tenant.vo.log.TenantOperateLog;
import com.rabbitmq.client.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 操作日志监听器
 * Created by sven on 2018-06-03
 */
@Component("tenantOperateLogListener")
public class TenantOperateLogListener implements ChannelAwareMessageListener {
    private static Logger logger = LogManager.getLogger(TenantOperateLogListener.class);

    @Autowired
    private TenantLogService tenantLogService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        logger.info("tenantOperateLogListener 收到了:{}", msg);

        try {
            //新增的MONGODB LOG
            TenantOperateLog operateLog = JSON.parseObject(msg, TenantOperateLog.class);
            tenantLogService.addTenantOperateLog(operateLog);
        } catch (Exception e) {
            logger.error("tenantOperateLogListener 收到了异常的JSON", e);
        }
    }
}
